<template>
    <div>
        <div>
            <h2>{{title}}</h2>
            <span>{{username}}<i>{{timer}}</i></span>
        </div>
        <div v-if="username===name">
            <button @click="edit">编辑</button>
            <button @click="del">删除</button>
        </div>
    </div>
</template>

<script>
export default {
    props:{
        title:String,
        timer:String,
        username:String,
        id:Number,
        con:String
    },
    created(){
        this.name = localStorage.getItem('username');
    },
    data(){
        return {
            name:''
        }
    },
    methods:{
        edit(){
            let {title,timer,username,id,con} = this;
            localStorage.setItem('blogCon',JSON.stringify({title,id,con}))
            this.$router.push({path:'/add'});
        },
        del(){
            this.$http.delete('/api/blog/'+this.id).then(res => {
                if(res.data.code === 1){
                    alert('删除成功');
                    this.$parent.getData();
                }
            })
        }
    }
}
</script>